import { getChannelByCountryApi, getWarehouseCodeByCountryIdApi, getCoumsterChannelByCountryApi } from "@/api/common";
import { getListApi } from "@/api/configCenter/countryRegion";
import { getListApi as getMasterialApi } from "@/api/configCenter/material";
import CommonHook from "@/hook/commonHook";
import { computed, reactive, ref } from "vue";

export default function InitHook() {
  const { getClassifyArrayByFields } = CommonHook();
  const formState = reactive<Partial<ClientAdvanceOrder.Form>>({
    shippingMethod: 0,
    expectedPickupTime: "",
    pickupAddressId: undefined,
    pickupAddress: undefined,
    warehouseId: undefined,
    warehouseName: undefined,
    estimatedWeight: undefined,
    estimatedVolume: undefined,
    bizPreOrderShipmentReqDtoList: [
      {
        channelId: undefined,
        countryRegionId: undefined,
        itemNumber: undefined,
        remark: undefined,
        // productName: undefined,
        // purchaseInsurance: undefined,
        // materialQualityId: [],
        materialQualityName: undefined,
        warehouseCode: undefined
      }
    ],
    bizSysFilesReqDtoList: []
  });
  const btnLoading = ref<boolean>(false);
  const drawerOpts = reactive<Base.DrawerOpts>({
    title: "",
    width: "520px",
    component: "",
    visible: false,
    type: 1,
    passData: {}
  });
  const submitTipFlag = ref<boolean>(false);
  const formDataRef = ref<any>(null);
  const waybillDataRef = ref<any>(null);
  const countryOpts = ref<Array<Base.GeneraDataType>>([]);
  const materialOpts = ref<Array<MaterialIndex.ListRows>>([]);
  const totalItemNumber = computed<number>(() => {
    if (formState.bizPreOrderShipmentReqDtoList?.length) {
      const allItemNum: Array<number> = formState.bizPreOrderShipmentReqDtoList?.map((t: any) => t.itemNumber || 0);
      return allItemNum.reduce((a: number, b: number) => a + b, 0);
    } else {
      return 0;
    }
  });
  const getAllCountryOpts = () => {
    return getListApi({ pageNum: 1, pageSize: 999999, status: 1 }).then((res: any) => {
      const { code, data } = res;
      if (code === 200) {
        countryOpts.value = data.records || [];
      }
    });
  };
  const getChannelOpts = (row: Base.GeneraDataType) => {
    return getChannelByCountryApi(row.countryRegionId).then((res: any) => {
      const { code, data } = res;
      if (code === 200) {
        row.channelOpts = getClassifyArrayByFields(data || [], "channelAllocation");
      }
    });
  };
  //过滤用户渠道
  const getCoumsterChannelByCountry = (row: Base.GeneraDataType) => {
    const customerId = JSON.parse(sessionStorage.getItem("user") as any).id;
    return getCoumsterChannelByCountryApi({
      countryRegionId: row.countryRegionId,
      customerId,
      warehouseCode: row.warehouseCode[1]
    }).then((res: any) => {
      const { code, data } = res;
      if (code === 200) {
        row.channelOpts = getClassifyArrayByFields(data || [], "channelAllocation", "disable");
      }
    });
  };
  const getAllMaterialOpts = () => {
    return getMasterialApi({ pageNum: 1, pageSize: 999999, isShow: 1, status: 1 } as MaterialIndex.SearchForm).then((res: any) => {
      const { code, data } = res;
      if (code === 200) {
        materialOpts.value = data.records || [];
      }
    });
  };
  const getAllWarehouseCodeOpts = (row: Base.GeneraDataType) => {
    return getWarehouseCodeByCountryIdApi(row.countryRegionId).then((res: any) => {
      const { code, data } = res;
      if (code === 200) {
        row.warehouseCodeOpts =
          data.map((t: any) => {
            return {
              ...t,
              warehouseCode: t.warehouseTypeName
            };
          }) || [];
      }
    });
  };
  return {
    formState,
    formDataRef,
    waybillDataRef,
    countryOpts,
    getAllCountryOpts,
    getChannelOpts,
    materialOpts,
    getAllMaterialOpts,
    totalItemNumber,
    drawerOpts,
    btnLoading,
    submitTipFlag,
    getAllWarehouseCodeOpts,
    getCoumsterChannelByCountry
  };
}
